package com.tangkeyon.util;

import org.apache.commons.net.ftp.FTPClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * @Author: tangkeyon@gmail.com
 * @Date: 2019/6/8 11:55
 * @Version 1.0
 * @Description: 上传文件到服务器的工具类
 */
public class FTPUtils {
    private static  String ftpIp=PropertiesUtil.getProperty("ftp.server.ip");
    private static  String ftpUser=PropertiesUtil.getProperty("ftp.user");
    private static  String ftpPass=PropertiesUtil.getProperty("ftp.pass");

    private String ip;
    private int port;
    private String user;
    private String pwd;
    private FTPClient ftpClient;

    public FTPUtils(String ip, int port, String user, String pwd) {
        this.ip = ip;
        this.port = port;
        this.user = user;
        this.pwd = pwd;
    }

    public static String getFtpIp() {
        return ftpIp;
    }

    public static void setFtpIp(String ftpIp) {
        FTPUtils.ftpIp = ftpIp;
    }

    public static String getFtpUser() {
        return ftpUser;
    }

    public static void setFtpUser(String ftpUser) {
        FTPUtils.ftpUser = ftpUser;
    }

    public static String getFtpPass() {
        return ftpPass;
    }

    public static void setFtpPass(String ftpPass) {
        FTPUtils.ftpPass = ftpPass;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public FTPClient getFtpClient() {
        return ftpClient;
    }

    public void setFtpClient(FTPClient ftpClient) {
        this.ftpClient = ftpClient;
    }

    /**
     * @Description: 将文件上传到服务器的指定文件夹
     * @param fileList 上传文件集合
     * @return
     * @throws IOException
     */
    public static boolean upload(List<File> fileList) throws IOException {
//        方法链调用
//         创建类对象为成员变量赋值
        FTPUtils ftpUtils=new FTPUtils(ftpIp,21,ftpUser,ftpPass);
        return ftpUtils.upload("img",fileList);
    }

    /**
     *@Description: 迭代文件集合通过IO操作上传到服务器
     * @param filePath
     * @param fileList
     * @return
     * @throws IOException
     */
    private boolean upload(String filePath,List<File> fileList) throws IOException {
        boolean flag=true;
        FileInputStream fileInputStream=null;
//        连接服务器成功     传入成员变量进行赋值
        if(connectServer(this.ip,this.port,this.user,this.pwd)){
            try {
                 ftpClient.changeWorkingDirectory(filePath);
                 ftpClient.setBufferSize(1024);
                 ftpClient.setControlEncoding("UTF-8");
                 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                 ftpClient.enterLocalPassiveMode();
                 for(File fileItem:fileList){
                     fileInputStream=new FileInputStream(fileItem);
                     ftpClient.storeFile(fileItem.getName(),fileInputStream);
                 }
            } catch (IOException e) {
                e.printStackTrace();
                flag=false;
            }finally {
                fileInputStream.close();
                ftpClient.disconnect();
            }
        }
                return flag;
    }

    /**
     * @Description: 与远程的服务器进行连接，并登录服务器
     * @param ip
     * @param port
     * @param user
     * @param pwd
     * @return
     */
    public boolean connectServer(String ip,int port,String user,String pwd){
        ftpClient=new FTPClient();
        boolean flag=true;
        try {
            ftpClient.connect(ip);
            flag = ftpClient.login(user, pwd);
        } catch (IOException e) {
            flag=false;
            e.printStackTrace();
        }
        return flag;
    }

}
